package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.POIUtils;
import com.ithema.service.OrderSettingService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {
    /**
     * 文件上传
     */
    @Reference
    private OrderSettingService orderSettingService;
    @RequestMapping("/upload")
  public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            ArrayList<OrderSetting> data = new ArrayList<>();
            for (String[] string : list) {
                String orderData = string[0];
                String number = string[1];

                OrderSetting orderSetting = new OrderSetting(new Date(orderData), Integer.parseInt(number));
                data.add(orderSetting);

            }
            //远程调用
            orderSettingService.upload(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

   @RequestMapping("/getOrderSettingByMonth")
   //根据月份查出对应的月份设置
    public  Result getOrderSettingByMonth(String date){ //date格式为yyyy-
       try {
           List<Map>list=orderSettingService.getOrderSettingByMonth(date);
           return  new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,list);
       } catch (Exception e) {
           e.printStackTrace();
          return  new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
       }


    }

    @RequestMapping("/editNumberByDate")
    //根据月份查出对应的月份设置
    public  Result editNumberByDate(@RequestBody OrderSetting orderSetting){ //date格式为yyyy-
        try {
          orderSettingService.editNumberByDate(orderSetting);
            return  new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }


    }

}
